import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

final class CMDInfo extends JPanel{
  private final JTextArea textArea=new JTextArea();
  private final JTextField commander=new JTextField();
  private final JScrollPane sp;
  private final Doer frame;
  CMDInfo(Doer frame){
    super(new BorderLayout());
    this.frame=frame;
    textArea.setEditable(false);
    add(sp=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
	                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),BorderLayout.CENTER);

    add(commander,BorderLayout.SOUTH);
    commander.addKeyListener(new KeyAdapter(){
                     public void keyTyped(KeyEvent e){
		       char c=e.getKeyChar();
		       if (c=='\n'){
			 try{
			   final Process process=Runtime.getRuntime().exec(commander.getText());
                           catchErr(process.getErrorStream());
                           catchErr(process.getInputStream());
			   textArea.append(commander.getText());
			 }
			 catch(IllegalArgumentException e1){textArea.append("\n");}
			 catch(IOException ex){
			   textArea.append("\n"+commander.getText()+" not found\n");
			 }
			 finally{commander.setText("");}
                       }
		     }
                   });
  }
  public synchronized void catchErr(final InputStream iS){
    new Thread(new Runnable(){
      public void run(){
        InputStreamReader iSR=new InputStreamReader(iS);
        final BufferedReader bR =new BufferedReader(iSR);
        String s=new String();
        try{
          while((s = bR.readLine()) != null) textArea.append(s+'\n');
          bR.close();
	      textArea.append("done\n");
		  sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMaximum());
        }
        catch(IOException e){System.out.print(e);}
      }
    }).start();
  }
  public JTextArea getTextArea(){
    return textArea;
  }
}
